<!DOCTYPE html>
<html>
<head>
    

    

    



    <meta charset="utf-8">
    
    
    
    <title>groovy基本语法 | Cucy的博客</title>
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    
    <meta name="theme-color" content="#3F51B5">
    
    
    <meta name="keywords" content="">
    <meta name="description" content="groovy基本语法: #!/usr/bin/env groovy // 打印 Hello Worldprintln “Hello world!”">
<meta property="og:type" content="article">
<meta property="og:title" content="groovy基本语法">
<meta property="og:url" content="http://zhourudong.cn/2016/11/14/8-groovy基本语法/index.html">
<meta property="og:site_name" content="Cucy的博客">
<meta property="og:description" content="groovy基本语法: #!/usr/bin/env groovy // 打印 Hello Worldprintln “Hello world!”">
<meta property="og:locale" content="zh-CN">
<meta property="og:updated_time" content="2017-09-08T07:02:37.073Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="groovy基本语法">
<meta name="twitter:description" content="groovy基本语法: #!/usr/bin/env groovy // 打印 Hello Worldprintln “Hello world!”">
    
        <link rel="alternate" type="application/atom+xml" title="Cucy的博客" href="/atom.xml">
    
    <link rel="shortcut icon" href="/favicon.ico">
    <link rel="stylesheet" href="/css/style.css?v=1.6.13">
    <script>window.lazyScripts=[]</script>

    <!-- custom head -->
    

</head>

<body>
    <div id="loading" class="active"></div>

    <aside id="menu" class="hide" >
  <div class="inner flex-row-vertical">
    <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menu-off">
        <i class="icon icon-lg icon-close"></i>
    </a>
    <div class="brand-wrap" style="background-image:url(/img/brand.jpg)">
      <div class="brand">
        <a href="/" class="avatar waves-effect waves-circle waves-light">
          <img src="/img/avatar.png">
        </a>
        <hgroup class="introduce">
          <h5 class="nickname">cucy</h5>
          <a href="mailto:292016176@qq.com" title="292016176@qq.com" class="mail">292016176@qq.com</a>
        </hgroup>
      </div>
    </div>
    <div class="scroll-wrap flex-col">
      <ul class="nav">
        
            <li class="waves-block waves-effect">
              <a href="/"  >
                <i class="icon icon-lg icon-home"></i>
                主页
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/archives"  >
                <i class="icon icon-lg icon-archives"></i>
                Archives
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="/tags"  >
                <i class="icon icon-lg icon-tags"></i>
                Tags
              </a>
            </li>
        
            <li class="waves-block waves-effect">
              <a href="https://github.com/cucy" target="_blank" >
                <i class="icon icon-lg icon-github"></i>
                Github
              </a>
            </li>
        
      </ul>
    </div>
  </div>
</aside>

    <main id="main">
        <header class="top-header" id="header">
    <div class="flex-row">
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light on" id="menu-toggle">
          <i class="icon icon-lg icon-navicon"></i>
        </a>
        <div class="flex-col header-title ellipsis">groovy基本语法</div>
        
        <div class="search-wrap" id="search-wrap">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="back">
                <i class="icon icon-lg icon-chevron-left"></i>
            </a>
            <input type="text" id="key" class="search-input" autocomplete="off" placeholder="输入感兴趣的关键字">
            <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="search">
                <i class="icon icon-lg icon-search"></i>
            </a>
        </div>
        
        
        <a href="javascript:;" class="header-icon waves-effect waves-circle waves-light" id="menuShare">
            <i class="icon icon-lg icon-share-alt"></i>
        </a>
        
    </div>
</header>
<header class="content-header post-header">

    <div class="container fade-scale">
        <h1 class="title">groovy基本语法</h1>
        <h5 class="subtitle">
            
                <time datetime="2016-11-14T08:55:54.000Z" itemprop="datePublished" class="page-time">
  2016-11-14
</time>


            
        </h5>
    </div>

    


</header>


<div class="container body-wrap">
    
<article id="post-8-groovy基本语法"
  class="post-article article-type-post fade" itemprop="blogPost">

    <div class="post-card">
        <h1 class="post-card-title">groovy基本语法</h1>
        <div class="post-meta">
            <time class="post-time" title="2016-11-14 16:55:54" datetime="2016-11-14T08:55:54.000Z"  itemprop="datePublished">2016-11-14</time>

            


            
<span id="busuanzi_container_page_pv" title="文章总阅读量" style='display:none'>
    <i class="icon icon-eye icon-pr"></i><span id="busuanzi_value_page_pv"></span>
</span>


        </div>
        <div class="post-content" id="post-content" itemprop="postContent">
            <p>groovy基本语法:</p>
<p>#!/usr/bin/env groovy</p>
<p>// 打印 Hello World<br>println “Hello world!”<br><a id="more"></a><br>// 变量定义 Variables: You can assign values to variables for later use<br>def x = 1<br>println x</p>
<p>x = new java.util.Date()<br>println x</p>
<p>x = -3.1499392<br>println x</p>
<p>x = false<br>println x</p>
<p>x = “Groovy!”<br>println x</p>
<p>// 列表 Creating an empty list<br>def technologies = []</p>
<p>/<strong><em> 添加一个元素到列表中 Adding a elements to the list </em></strong>/<br>// As with Java<br>technologies.add(“Grails”)</p>
<p>// 添加多个元素到列表中 Add multiple elements<br>technologies.addAll([“Gradle”,”Griffon”])</p>
<p>/<strong><em> 删除列表中元素 Removing elements from the list </em></strong>/<br>// As with Java<br>technologies.remove(“Griffon”)</p>
<p>// Subtraction works also<br>technologies = technologies - ‘Grails’</p>
<p>/<strong><em> Iterating Lists </em></strong>/<br>// Iterate over elements of a list<br>technologies.each { println “Technology: $it”}<br>technologies.eachWithIndex { it, i -&gt; println “$i: $it”}</p>
<p>/<strong><em> 元素是否存在判断 Checking List contents </em></strong>/<br>//Evaluate if a list contains element(s) (boolean)<br>contained = technologies.contains( ‘Groovy’ )</p>
<p>// Or<br>contained = ‘Groovy’ in technologies</p>
<p>// 计数 Check for multiple contents<br>technologies.containsAll([‘Groovy’,’Grails’])</p>
<p>/<strong><em> 排序 Sorting Lists </em></strong>/</p>
<p>// 在原列表上重新排序 Sort a list (mutates original list)<br>technologies.sort()</p>
<p>// 排序不影响源列表 To sort without mutating original, you can do:<br>sortedTechnologies = technologies.sort( false )</p>
<p>/<strong><em> 字典 Manipulating Lists </em></strong>/</p>
<p>// 替换列表中所有元素  Replace all elements in the list<br>Collections.replaceAll(technologies, ‘Gradle’, ‘gradle’)</p>
<p>//Shuffle a list<br>Collections.shuffle(technologies, new Random())</p>
<p>//Clear a list<br>technologies.clear()</p>
<p>// 字典 Creating an empty map<br>def devMap = [:]</p>
<p>// 添加新元素 Add values<br>devMap = [‘name’:’Roberto’, ‘framework’:’Grails’, ‘language’:’Groovy’]<br>devMap.put(‘lastName’,’Perez’)</p>
<p>// 遍历字典 Iterate over elements of a map<br>devMap.each { println “$it.key: $it.value” }<br>devMap.eachWithIndex { it, i -&gt; println “$i: $it”}</p>
<p>// 判断是否为存在key Evaluate if a map contains a key<br>assert devMap.containsKey(‘name’)</p>
<p>// 判断是否为存在value Evaluate if a map contains a value<br>assert devMap.containsValue(‘Roberto’)</p>
<p>//Get the keys of a map<br>println devMap.keySet()</p>
<p>//Get the values of a map<br>println devMap.values()</p>
<p>//Groovy supports the usual if - else syntax<br>// if 语句<br>def x1 = 3</p>
<p>if(x1==1) {<br>    println “One”<br>} else if(x1==2) {<br>    println “two”<br>} else {<br>    println “X greater than Two”<br>}</p>
<p>//Groovy also supports the ternary operator:<br>def y = 10<br>def x2 = (y &gt; 1) ? “worked” : “failed”<br>assert x2 == “worked”</p>
<p>//Instead of using the ternary operator:<br>//displayName = user.name ? user.name : ‘Anonymous’<br>//We can write it:<br>//displayName = user.name ?: ‘Anonymous’</p>
<p>//For loop<br>//Iterate over a range<br>def x3 = 0<br>for (i in 0 .. 30) {<br>x3 += i<br>}</p>
<p>//Iterate over a list<br>x4 = 0<br>for( i in [5,3,2,1] ) {<br>x4 += i<br>}</p>
<p>//Iterate over an array<br>array = (0..20).toArray()<br>x5 = 0<br>for (i in array) {<br>x5 += i<br>}</p>
<p>//Iterate over a map<br>def map = [‘name’:’Roberto’, ‘framework’:’Grails’, ‘language’:’Groovy’]<br>x6 = 0<br>for ( e in map ) {<br>x6 += e.value<br>}</p>
<p>/<em><br>Closures<br>A Groovy Closure is like a “code block” or a method pointer. It is a piece of<br>code that is defined and then executed at a later point.<br>More info at: <a href="http://www.groovy-lang.org/closures.html" target="_blank" rel="external">http://www.groovy-lang.org/closures.html</a>
</em>/</p>
<p>//Example:<br>def clos = { println “Hello World!” }</p>
<p>println “Executing the Closure:”<br>clos()</p>
<p>//Passing parameters to a closure<br>def sum = { a, b -&gt; println a+b }<br>sum(2,4)</p>
<p>//Closures may refer to variables not listed in their parameter list.<br>def x7 = 5<br>def multiplyBy = { num -&gt; num * x7 }<br>println multiplyBy(10)</p>
<p>// If you have a Closure that takes a single argument, you may omit the<br>// parameter definition of the Closure<br>def clos2 = { println it }<br>clos2( “hi” )</p>
<p>/<em><br>Groovy can memoize closure results [1][2][3]
</em>/<br>def cl = {a, b -&gt;<br>sleep(3000) // simulate some time consuming processing<br>a + b<br>}</p>
<p>mem = cl.memoize()</p>
<p>def callClosure(a, b) {<br>def start = System.currentTimeMillis()<br>println mem(a, b)<br>println “Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs.”<br>}</p>
<p>callClosure(1, 2)<br>callClosure(1, 2)<br>callClosure(2, 3)<br>callClosure(2, 3)<br>callClosure(3, 4)<br>callClosure(3, 4)<br>callClosure(1, 2)<br>callClosure(2, 3)<br>callClosure(3, 4)</p>

        </div>

        <blockquote class="post-copyright">
    <div class="content">
        
<span class="post-time">
    最后更新时间：<time datetime="2017-09-08T07:02:37.073Z" itemprop="dateUpdated">2017-09-08 15:02:37</time>
</span><br>


        
        <a href="/2016/11/14/8-groovy基本语法/" target="_blank" rel="external">http://zhourudong.cn/2016/11/14/8-groovy基本语法/</a>
        
    </div>
    <footer>
        <a href="http://zhourudong.cn">
            <img src="/img/avatar.png" alt="cucy">
            cucy
        </a>
    </footer>
</blockquote>

        
<div class="page-reward">
    <a id="rewardBtn" href="javascript:;" class="page-reward-btn waves-effect waves-circle waves-light">赏</a>
</div>



        <div class="post-footer">
            

            
<div class="page-share-wrap">
    

<div class="page-share" id="pageShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://zhourudong.cn/2016/11/14/8-groovy基本语法/&title=《groovy基本语法》 — Cucy的博客&pic=http://zhourudong.cn/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://zhourudong.cn/2016/11/14/8-groovy基本语法/&title=《groovy基本语法》 — Cucy的博客&source=groovy基本语法:
#!/usr/bin/env groovy
// 打印 Hello Worldprintln “Hello world!”" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://zhourudong.cn/2016/11/14/8-groovy基本语法/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《groovy基本语法》 — Cucy的博客&url=http://zhourudong.cn/2016/11/14/8-groovy基本语法/&via=http://zhourudong.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://zhourudong.cn/2016/11/14/8-groovy基本语法/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>



    <a href="javascript:;" id="shareFab" class="page-share-fab waves-effect waves-circle">
        <i class="icon icon-share-alt icon-lg"></i>
    </a>
</div>



        </div>
    </div>

    
<nav class="post-nav flex-row flex-justify-between">
  
    <div class="waves-block waves-effect prev">
      <a href="/2016/11/17/9-shell-常用/" id="post-prev" class="post-nav-link">
        <div class="tips"><i class="icon icon-angle-left icon-lg icon-pr"></i> Prev</div>
        <h4 class="title">常用</h4>
      </a>
    </div>
  

  
    <div class="waves-block waves-effect next">
      <a href="/2016/11/14/7-Dockerfile/" id="post-next" class="post-nav-link">
        <div class="tips">Next <i class="icon icon-angle-right icon-lg icon-pl"></i></div>
        <h4 class="title">Dockerfile</h4>
      </a>
    </div>
  
</nav>



    














</article>

<div id="reward" class="page-modal reward-lay">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <h3 class="reward-title">
        <i class="icon icon-quote-left"></i>
        谢谢大爷~
        <i class="icon icon-quote-right"></i>
    </h3>
    <div class="reward-content">
        
        <div class="reward-code">
            <img id="rewardCode" src="/img/wechat.jpg" alt="打赏二维码">
        </div>
        
        <label class="reward-toggle">
            <input id="rewardToggle" type="checkbox" class="reward-toggle-check"
                data-wechat="/img/wechat.jpg" data-alipay="/img/alipay.jpg">
            <div class="reward-toggle-ctrol">
                <span class="reward-toggle-item wechat">微信</span>
                <span class="reward-toggle-label"></span>
                <span class="reward-toggle-item alipay">支付宝</span>
            </div>
        </label>
        
    </div>
</div>



</div>

        <footer class="footer">
    <div class="top">
        
<p>
    <span id="busuanzi_container_site_uv" style='display:none'>
        站点总访客数：<span id="busuanzi_value_site_uv"></span>
    </span>
    <span id="busuanzi_container_site_pv" style='display:none'>
        站点总访问量：<span id="busuanzi_value_site_pv"></span>
    </span>
</p>


        <p>
            
                <span><a href="/atom.xml" target="_blank" class="rss" title="rss"><i class="icon icon-lg icon-rss"></i></a></span>
            
            <span>博客内容遵循 <a rel="license" href="https://creativecommons.org/licenses/by-nc-sa/4.0/deed.zh">知识共享 署名 - 非商业性 - 相同方式共享 4.0 国际协议</a></span>
        </p>
    </div>
    <div class="bottom">
        <p><span>cucy &copy; 2016 - 2017</span>
            <span>
                
                Power by <a href="http://hexo.io/" target="_blank">Hexo</a> Theme <a href="https://github.com/yscoder/hexo-theme-indigo" target="_blank">indigo</a>
            </span>
        </p>
    </div>
</footer>

    </main>
    <div class="mask" id="mask"></div>
<a href="javascript:;" id="gotop" class="waves-effect waves-circle waves-light"><span class="icon icon-lg icon-chevron-up"></span></a>



<div class="global-share" id="globalShare">
    <ul class="reset share-icons">
      <li>
        <a class="weibo share-sns" target="_blank" href="http://service.weibo.com/share/share.php?url=http://zhourudong.cn/2016/11/14/8-groovy基本语法/&title=《groovy基本语法》 — Cucy的博客&pic=http://zhourudong.cn/img/avatar.png" data-title="微博">
          <i class="icon icon-weibo"></i>
        </a>
      </li>
      <li>
        <a class="weixin share-sns wxFab" href="javascript:;" data-title="微信">
          <i class="icon icon-weixin"></i>
        </a>
      </li>
      <li>
        <a class="qq share-sns" target="_blank" href="http://connect.qq.com/widget/shareqq/index.html?url=http://zhourudong.cn/2016/11/14/8-groovy基本语法/&title=《groovy基本语法》 — Cucy的博客&source=groovy基本语法:
#!/usr/bin/env groovy
// 打印 Hello Worldprintln “Hello world!”" data-title=" QQ">
          <i class="icon icon-qq"></i>
        </a>
      </li>
      <li>
        <a class="facebook share-sns" target="_blank" href="https://www.facebook.com/sharer/sharer.php?u=http://zhourudong.cn/2016/11/14/8-groovy基本语法/" data-title=" Facebook">
          <i class="icon icon-facebook"></i>
        </a>
      </li>
      <li>
        <a class="twitter share-sns" target="_blank" href="https://twitter.com/intent/tweet?text=《groovy基本语法》 — Cucy的博客&url=http://zhourudong.cn/2016/11/14/8-groovy基本语法/&via=http://zhourudong.cn" data-title=" Twitter">
          <i class="icon icon-twitter"></i>
        </a>
      </li>
      <li>
        <a class="google share-sns" target="_blank" href="https://plus.google.com/share?url=http://zhourudong.cn/2016/11/14/8-groovy基本语法/" data-title=" Google+">
          <i class="icon icon-google-plus"></i>
        </a>
      </li>
    </ul>
 </div>


<div class="page-modal wx-share" id="wxShare">
    <a class="close" href="javascript:;"><i class="icon icon-close"></i></a>
    <p>扫一扫，分享到微信</p>
    <img src="//api.qrserver.com/v1/create-qr-code/?data=http://zhourudong.cn/2016/11/14/8-groovy基本语法/" alt="微信分享二维码">
</div>




    <script src="//cdn.bootcss.com/node-waves/0.7.4/waves.min.js"></script>
<script>
var BLOG = { ROOT: '/', SHARE: true, REWARD: true };


</script>

<script src="/js/main.min.js?v=1.6.13"></script>


<div class="search-panel" id="search-panel">
    <ul class="search-result" id="search-result"></ul>
</div>
<template id="search-tpl">
<li class="item">
    <a href="{path}" class="waves-block waves-effect">
        <div class="title ellipsis" title="{title}">{title}</div>
        <div class="flex-row flex-middle">
            <div class="tags ellipsis">
                {tags}
            </div>
            <time class="flex-col time">{date}</time>
        </div>
    </a>
</li>
</template>

<script src="/js/search.min.js?v=1.6.13" async></script>






<script async src="//dn-lbstatics.qbox.me/busuanzi/2.3/busuanzi.pure.mini.js"></script>



<script>
(function() {
    var OriginTitile = document.title, titleTime;
    document.addEventListener('visibilitychange', function() {
        if (document.hidden) {
            document.title = '死鬼去哪里了！';
            clearTimeout(titleTime);
        } else {
            document.title = '(つェ⊂)咦!又好了!';
            titleTime = setTimeout(function() {
                document.title = OriginTitile;
            },2000);
        }
    });
})();
</script>



</body>
</html>
